// pages/mine/mine.js
const app = getApp();
const api = require('../../../utils/api.js');
const {myRequest} = require('../../../utils/myRequest.js');
Page({
  data: {
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    typeIndex:null,
    more:[],
    footact:null,
    currentPage:1
  },
  // 跳转管理收货地址页面
  toAddress(){
    wx.navigateTo({
      url: '../../subPages/addressManage/addressManage',
      success: (result)=>{
        console.log(result);
      },
      fail: (err)=>{console.log(err);},
      complete: ()=>{}
    });
  },
  // 获取收藏列表
  collect(e) {
    // console.log(this.data.token)
    myRequest({
      url: api.collectList,
      data:{
        typeId: 0,
      },
      method:'GET',
      success: (res) => {
        console.log(res);
        let collect = res.data.data;
        let collectList = res.data.data.data;
        let more=[];
        // console.log(footmark.length)
        for(let i=0;i<collectList.length;i++){
          more[i]=false;
        }
        this.setData({
          collect:collect,
          collectList:collectList,
          more:more,
          typeIndex:0,
          currentPage:1
        })
      },
      fail: (err) => {
        console.log(err);
      }
    })
  },
  // 获取订单列表
  order(){
    myRequest({
      url:api.orderList,
      data:{

      },
      method:'GET',
      success:(res)=>{
        console.log(res);
      },
      fail:(err)=>{
        console.log(err);
      }
    })
  },
  delete(e){
    let footprintId = e.currentTarget.dataset.id;
    console.log(footprintId);
    myRequest({
      url:api.footdelete,
      data:{
        footprintId:footprintId
      },
      method:'POST',
      success:(res)=>{
        console.log(res);
        wx.showToast({
          title:'已删除足迹'
        })
        this.footmark();
      }
    })
  },
  addcollect(e){
    console.log(e);
    let goods_id=e.currentTarget.dataset.id;
    let typeId=0;
    myRequest({
      url:api.addordelete,
      data:{
        typeId:typeId,
        valueId:goods_id
      },
      method:'POST',
      success:(res)=>{
        console.log(res);
        let collectImg;
        if(res.data.data.type=='add'){
          collectImg = '../../../assests/icon/collect.png';
          wx.showToast({
            title:'已成功添加收藏'
          })
          this.footmark();
        }else{
          collectImg = '../../../assests/icon/uncollect.png';
          wx.showToast({
            title:'已取消收藏'
          })
          this.footmark();
        }
        this.setData({
          collectImg:collectImg
        })
      },
      fail:(err)=>{
        console.log(err);
      }
    })
  },
  // 收藏列表页面
  delcollect(e){
    console.log(e);
    let goods_id=e.currentTarget.dataset.id;
    let typeId=0;
    myRequest({
      url:api.addordelete,
      data:{
        typeId:typeId,
        valueId:goods_id
      },
      method:'POST',
      success:(res)=>{
        console.log(res);
        let collectImg;
        if(res.data.data.type!='add'){
          // collectImg = '../../../assests/icon/collect.png';
          wx.showToast({
            title:'已取消收藏'
          })
          this.collect();
        }
        // this.setData({
        //   collectImg:collectImg
        // })
      },
      fail:(err)=>{
        console.log(err);
      }
    })
  },
  // 隐藏或显示收藏/删除按钮
  changemore(e){
    console.log(e);
    let currentid,ind;
    if(e.currentTarget.dataset.id){
       currentid = e.currentTarget.dataset.id;
       ind = e.currentTarget.dataset.index;
      console.log(ind);
    }else{
       ind = this.data.ind;
       console.log(ind);
    }
    let more = this.data.more;
    more[ind] = !more[ind];
    if(more[ind]){
      myRequest({
        url:api.collectList,
        data:{
          typeId:0
        },
        success:(res)=>{
          console.log(res);
          let collected = res.data.data.data;
          let collectImg = '../../../assests/icon/uncollect.png';
          collected.some(function(item,index){
              if(item.value_id == currentid){
                collectImg='../../../assests/icon/collect.png';
              }
          })
          this.setData({
            more:more,
            collectImg:collectImg,
            ind:ind
          })
        }
      })
    }else{
      this.setData({
        more:more,
        ind:ind
      })
    }
    
  },
  // 选择其他时间的足迹
  chooseTime(e){
    let footact =  e.currentTarget.dataset.index;
    this.setData({
      footact:footact
    })
    this.footmark();
  },
  // 获取足迹列表
  footmark(){
    myRequest({
      url:api.footprint,
      data:{

      },
      method: 'GET',
      success: (res) => {
        console.log(res);
        let footmark = res.data.data;
        // let timeList=footmark.data;
        // timeList.reverse();
        // if(this.data.footact==null){
        //   let footact=timeList.length-1;
        //   this.setData({
        //     footact:footact
        //   })
        // }
        let footmarkList = res.data.data.data[0];
        
        
        let more=[];
        // console.log(footmark.length)
        for(let i=0;i<footmarkList.length;i++){
          more[i]=false;
        }
        console.log(more);
        this.setData({
          typeIndex:2,
          footmark:footmark,
          footmarkList:footmarkList,
          more:more,
          currentPage:1
          // timeList:timeList,
        })
        // console.log(footmarkList.length);

      },
      fail: (err) => {
        console.log(err);
      }
    })
  },
  getUserInfo(e) {
    console.log('用户主动授权', e, app.globalData.code);
    // 保存在app内
    app.globalData.userInfo = e.detail;
    // 保存在mine页面内
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
    // 向login接口发出请求
    myRequest({
      url: api.login,
      data: {
        appId: 'wxa6b66ff12cd59005',
        secret: '27914362cec3d2efe4e2f55a77d250c1',
        code: app.globalData.code,
        userInfo: app.globalData.userInfo
      },
      // header: { 'content-type': 'application/json' },
      method: 'POST',
      success: (res) => {
        console.log(res.data);
        if (res.data.data.token) {
          // 如果有token
          // 储存数据
          wx.setStorage({
            key: 'token',
            data: res.data.data.token,
            success: (result) => {
              console.log('token保存成功', result);
            },
            fail: (err) => {
              console.log(err)
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo;
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    // wx.getStorage({
    //   key: 'token',
    //   success: (res)=>{
    //     console.log(res);
    //     // app.globalData.token = res.data;
    //     // console.log(app.globalData);
    //     this.setData({
    //       token:res.data
    //     })
    //   },
    // })
    this.collect();
    // this.footmark();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    wx.showLoading({
      title: '加载中...',
      duration: 1000,
    });
    let url=[api.collectList,api.orderList,api.footprint];
    let typeIndex = this.data.typeIndex;
    let currentPage=this.data.currentPage+1;
    this.setData({
      currentPage:currentPage
    })
    myRequest({
      url:url[typeIndex],
      data:{
        typeId:0,
        page:currentPage,
      },
      success:(res)=>{
        if(typeIndex==2){
          console.log(res);
          if (res.data.data.currentPage != this.data.currentPage - 1){
            let footmarkList=this.data.footmarkList.concat(res.data.data.data[0]);
            setTimeout(() => {
              this.setData({
                // 数据拼接
                footmarkList:footmarkList
              })
            }, 1200)
          }else{
            let page = this.data.currentPage - 1;
            this.setData({
              currentPage: page,
            })
            wx.hideLoading();
          wx.showToast({
            title: '已无更多内容啦~',
            icon: 'none',
            duration: 3000,
          });
          return ;
          }
        }else if(typeIndex==1){
          if (res.data.data.currentPage != this.data.currentPage - 1){
            let orderList =this.data.orderList.concat(res.data.data.data);
            setTimeout(() => {
              this.setData({
                // 数据拼接
                orderList:orderList
              })
            }, 1200)
          }else{
            let page = this.data.currentPage - 1;
            this.setData({
              currentPage: page,
            })
            wx.hideLoading();
          wx.showToast({
            title: '已无更多内容啦~',
            icon: 'none',
            duration: 3000,
          });
          return ;
          }
        }else{
          console.log(res.data.data.currentPage);
          if(res.data.data.currentPage!=this.data.currentPage-1){
            let collectList =this.data.collectList.concat(res.data.data.data);
            setTimeout(() => {
              this.setData({
                // 数据拼接
                collectList:collectList
              })
            }, 1200)
          }else{
            let page = this.data.currentPage-1;
            this.setData({
              currentPage:page,
            })
            wx.hideLoading();
          wx.showToast({
            title: '已无更多内容啦~',
            icon: 'none',
            duration: 3000,
          });
          return ;
          }
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})